package com.soft.controller;

import com.soft.pojo.PageBean;
import com.soft.pojo.Workon;
import com.soft.service.OutWorkService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-26 15:26
 */
@WebServlet("/showAllOutWorkByPage")
public class ShowAllOutWorkByPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OutWorkService outWorkService = new OutWorkService();
        PageBean<Workon> outWorkPageBean = new PageBean<>();

        int currentPage = 1;
        String currentPage_ = req.getParameter("currentPage");
        if (currentPage_ != null) {
            currentPage = Integer.parseInt(currentPage_);
        }

        int currentCount = 10;

        outWorkPageBean.setCurrentPage(currentPage);
        outWorkPageBean.setCurrentCount(currentCount);
        outWorkPageBean.setTotalCount(outWorkService.findOutWorkCount());
        outWorkPageBean.setTotalPage((int) Math.ceil(outWorkService.findOutWorkCount() * 1.0 / currentCount));
        outWorkPageBean.setList(outWorkService.findOutWorkByPage((currentPage - 1) * currentCount, currentCount));

        req.setAttribute("workOutPg", outWorkPageBean);
        req.getRequestDispatcher("outwork.jsp").forward(req, resp);
    }
}
